package com.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.CommonResult;
import com.mall.model.entity.Admin;
import com.mall.service.AdminService;
import com.mall.utils.MD5Util;
import com.mall.vo.queryVo.adminQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author 李茂鑫
 * @Date 2025/1/20 10:42
 * @Descripition
 */
@Tag(name = "管理员控制器")
@RequestMapping("/admin")
@RestController
public class AdminController {
    @Resource
    private AdminService adminService;

    /**
     * 分页查询用户
     * @param adminQueryVo
     * @return
     */
    @PostMapping("/listAll")
    @CrossOrigin
    @Operation(summary = "分页查询用户")
    public CommonResult listAdmin(adminQueryVo adminQueryVo) {
        IPage<Admin> page=new Page(adminQueryVo.getPageNo(),adminQueryVo.getPageSize());
        //调用分页方法
        adminService.findUserListByPage(page,adminQueryVo);
        return CommonResult.success(page);
    }

    /**
     * 添加用户
     * @param admin
     * @return
     */
    @PostMapping("/add")
    @CrossOrigin
    @Operation(summary = "添加用户")
    public CommonResult add(@RequestBody Admin admin) {
        admin.setPwd(MD5Util.getMD5String(admin.getPwd()));
        //查询用户
        Admin item = adminService.findAdminByAdminId(admin.getId());

        //判断用户是否存在
        if (item != null) {
//            return new CommonResult(500, "用户已存在",false);
            return CommonResult.error("用户已存在");
        }
        //调用添加方法
        if (adminService.save(admin)) {
//            return new CommonResult(200, "用户添加成功",true);
            return CommonResult.success("用户添加成功");
        }
        return new CommonResult(500, "用户添加失败",false);
    }

    /**
     * 更新用户
     * @param admin
     * @return
     */
    @PutMapping("/update")
    @CrossOrigin
    @Operation(summary = "更新用户")
    public CommonResult update(@RequestBody Admin admin) {
        //查询用户
        Admin item = adminService.findAdminByAdminId(admin.getId());
        //判断对象是否为空,且查询到的用户ID不等于当前编辑的用户ID，表示该名称被占用
        if (item != null && ! Objects.equals(item.getId(), admin.getId())) {
            return new CommonResult(500, "用户已存在",false);
        }
        //调用修改方法
        if (adminService.updateById(admin)) {
            return  new CommonResult(200, "用户修改成功",true);
        } else return new CommonResult(500, "用户修改失败",false);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    @Operation(summary = "删除用户")
    public CommonResult delete(@PathVariable String id) {
        boolean item = AdminService.deleteById(id);
        //调用删除方法
        if (AdminService.deleteById(id)) {
            return  new CommonResult(200, "用户删除成功",true);
        } else return new CommonResult(500, "用户删除失败",false);
    }

}
